package Customer;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class CustomerManager {
    Map<String, Customer> customers = new HashMap<>();

    public void registerCustomer(String username, String password, String email) {
        if (!Customer.validateUsername(username)) {
            System.out.println("用户名长度必须至少为 5 个字符。");
            return;
        }
        if (!Customer.validatePassword(password)) {
            System.out.println("密码长度必须至少为 8 个字符，并且包含大小写字母、数字和符号的组合。");
            return;
        }
        Customer customer = new Customer(username, password, email);
        customers.put(username, customer);
        System.out.println("客户注册成功。");
    }

    public boolean login(String username, String password) {
        Customer customer = customers.get(username);
        if (customer == null || customer.isLocked()) {
            System.out.println("帐户未找到或已锁定。");
            return false;
        }

        if (customer.getPassword().equals(password)) {
            customer.resetFailedLoginAttempts();
            System.out.println("成功登入。");
            return true;
        } else {
            customer.incrementFailedLoginAttempts();
            System.out.println("密码错误！");
            return false;
        }
    }

    public void forgotPassword(String username, String email) {
        Customer customer = customers.get(username);
        if (customer != null && customer.getEmail().equals(email)) {
            String newPassword = generateRandomPassword();
            customer.setPassword(newPassword);
            // 模拟通过电子邮件发送新密码
            System.out.println("新密码已被传到邮箱: " + newPassword);
        } else {
            System.out.println("用户名或邮箱错误。");
        }
    }

    private String generateRandomPassword() {
        // 简单的随机密码生成器（用于演示目的）
        return "NewP@ssw0rd";
    }

    public void changePassword(Customer customer, String newPassword) {
        if (Customer.validatePassword(newPassword)) {
            customer.setPassword(newPassword);
            System.out.println("密码更改成功。");
        } else {
            System.out.println("密码长度必须至少为 8 个字符，并且包含大小写字母、数字和符号的组合。");
        }
    }

    public void forgotPassword(CustomerManager manager, String username, String email) {
        manager.forgotPassword(username, email);
    }

}



